package com.societyGames.flashForms
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;

public class VerticalSplitContainer extends Sprite
{
  private var _desiredHeight:Number;
  private var _desiredWidth:Number;
  private var _leftPercent:Number = Number.NaN;
  private var _leftItem:DisplayObject;
  private var _splitter:Splitter;
  private var _rightItem:DisplayObject;

  public function VerticalSplitContainer(splitter:DisplayObject, leftRightCursor:DisplayObject, leftPercent:Number = 0.5)
  {
    if (splitter == null)
    {
      throw new ArgumentError("Argument \'splitter\' cannot be null.");
    }
    this._splitter = new Splitter(splitter, leftRightCursor);
    this._splitter.y = 0;
    this._splitter.bounds = new Rectangle();
    this._splitter.droppedEventObject.addEventListener(splitterDroppedHandler);
    this.addChild(this._splitter);
    this._leftPercent = leftPercent;
    this._desiredWidth = width;
    this._desiredHeight = height;
    this.height = this._desiredHeight;
    this.width = this._desiredWidth;
  }

  public function set leftPercent(value:Number):void
  {
    if (this._leftPercent != value)
    {
      if (isNaN(value))
      {
        throw new ArgumentError("Value cannot be NaN.");
      }
      else if (value < 0)
      {
        value = 0;
      }
      else if (value > 1)
      {
        value = 1;
      }
      this._leftPercent = value;
      recalculateWidth();
    }
  }

  public function get leftPercent():Number
  {
    return this._leftPercent;
  }

  public function get leftItem():DisplayObject
  {
    return this._leftItem;
  }

  public function set leftItem(value:DisplayObject):void
  {
    if (this._leftItem != value)
    {
      if (this._leftItem != null)
      {
        this.removeChild(this._leftItem);
      }
      this._leftItem = value;
      if (this._leftItem != null)
      {
        this._leftItem.y = 0;
        this._leftItem.height = this._desiredHeight;
        this.addChildAt(this._leftItem, 0);
        recalculateWidth();
      }
    }
  }

  public function get rightItem():DisplayObject
  {
    return this._rightItem;
  }

  public function set rightItem(value:DisplayObject):void
  {
    if (this._rightItem != value)
    {
      if (this._rightItem != null)
      {
        this.removeChild(this._rightItem);
      }
      this._rightItem = value;
      if (this._rightItem != null)
      {
        this._rightItem.y = 0;
        this._rightItem.height = this._desiredHeight;
        this.addChildAt(this._rightItem, 0);
        recalculateWidth();
      }
    }
  }

  public override function set height(value:Number):void
  {
    if (value != this._desiredHeight)
    {
      this._desiredHeight = value;
      if (this._leftItem != null)
      {
        this._leftItem.height = value;
      }
      this._splitter.height = value;
      if (this._rightItem != null)
      {
        this._rightItem.height = value;
      }
    }
  }

  private function recalculateWidth():void
  {
    var leftItemWidth:Number = (this._desiredWidth - this._splitter.width) * this._leftPercent;
    if (this._leftItem != null)
    {
      this._leftItem.x = 0;
      this._leftItem.width = leftItemWidth;
    }
    this._splitter.x = leftItemWidth;
    this._splitter.bounds.width = this._desiredWidth;
    if (this._rightItem != null)
    {
      this._rightItem.x = this._splitter.x + this._splitter.width;
      this._rightItem.width = this._desiredWidth - this._splitter.width - leftItemWidth;
    }
  }

  public override function set width(value:Number):void
  {
    if (value != this._desiredWidth)
    {
      this._desiredWidth = value;
      recalculateWidth();
    }
  }

  private function splitterDroppedHandler(event:Event):void
  {
    this.leftPercent = this._splitter.x / (this._desiredWidth - this._splitter.width);
  }
}
}